package __Colly

import (
	"fmt"
	"github.com/gocolly/colly"
)

func main() {
	c := colly.NewCollector(
		colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"),
	)
	c.OnHTML(".sidebar-link", func(e *colly.HTMLElement) {
		href := e.Attr("href")
		if href != "index.html" {
			c.Visit(e.Request.AbsoluteURL(href))
		}
	})
	c.OnHTML(".article-title", func(h *colly.HTMLElement) {
		title := h.Text
		fmt.Println(title)
	})
	c.OnHTML(".article", func(l *colly.HTMLElement) {
		html, _ := l.DOM.Html()
		fmt.Println(html)
	})
	c.OnRequest(func(r *colly.Request) {
		fmt.Println(r.URL.String())
	})
	c.Visit("https://gorm.io/zh_CN/docs/")
}
